//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;
using System.Text.RegularExpressions;

namespace CommunityServer.Files.Components
{
	/// <summary>
	/// This class defines the properties that are used for Galleries.  It inherits from the base class <see cref="CommunityServer.Components.Section"/>.
	/// </summary>
	public class Folder : CommunityServer.Components.Section
	{

		#region FoldersSortBy Stuff

		public enum FoldersSortBy 
		{
			Subject,
			SubjectDesc,
			Author,
			AuthorDesc,
			Comments,
			CommentsDesc,
			Views,
			ViewsDesc,
			ThreadDate,
			ThreadDateDesc,
			Rating,
			RatingDesc
		}

		private static FoldersSortBy _sortBy = FoldersSortBy.ThreadDateDesc;

		public static FoldersSortBy GSortBy
		{
			get { return _sortBy; }
			set { _sortBy = value; }
		}

		#endregion

		#region CompareTo



		#endregion

		#region Permissions

        public override PermissionBase DefaultRolePermission
        {
            get { return new FileGalleryPermission(); }
        }

        public override AccessCheckDelegate AccessCheck
        {
            get { return new AccessCheckDelegate(FileGalleryPermission.AccessCheck); }
        }

        public override ValidatePermissionsDelegate ValidatePermissions
        {
            get { return new ValidatePermissionsDelegate(FileGalleryPermission.Validate); }
        }

	    #endregion

		#region Feature Settings

		public bool EnableRssSyndication
		{
			get
			{
				string val = GetExtendedAttribute( "EnableRssSyndication" );
				if(val != string.Empty)
					return bool.Parse(val);
				else
					return true;
			}
			set { SetExtendedAttribute( "EnableRssSyndication", value.ToString() ); }
		}

		public bool EnableTagsRss
		{
			get
			{
				string val = GetExtendedAttribute( "EnableTagsRss" );
				if(val != string.Empty)
					return bool.Parse(val);
				else
					return true;
			}
			set { SetExtendedAttribute( "EnableTagsRss", value.ToString() ); }
		}

		public bool EnableComments
		{
			get
			{
				string val = GetExtendedAttribute( "EnableComments" );
				if(val != string.Empty)
					return bool.Parse(val);
				else
					return true;
			}
			set { SetExtendedAttribute( "EnableComments", value.ToString() ); }
		}

		public bool ModerateComments
		{
			get
			{
				string val = GetExtendedAttribute( "ModerateComments" );
				if(val != string.Empty)
					return bool.Parse(val);
				else
					return false;
			}
			set { SetExtendedAttribute( "ModerateComments", value.ToString() ); }
		}

		public bool EnableCommentNotification
		{
			get
			{
				string val = GetExtendedAttribute( "EnableCommentNotification" );
				if(val != string.Empty)
					return bool.Parse(val);
				else
					return false;
			}
			set { SetExtendedAttribute( "EnableCommentNotification", value.ToString() ); }
		}

		public bool EnableEntryNotification
		{
			get
			{
				string val = GetExtendedAttribute( "EnableEntryNotification" );
				if(val != string.Empty)
					return bool.Parse(val);
				else
					return false;
			}
			set { SetExtendedAttribute( "EnableEntryNotification", value.ToString() ); }
		}

		public bool EnableRatings
		{
			get
			{
				string val = GetExtendedAttribute( "EnableRatings" );
				if(val != string.Empty)
					return bool.Parse(val);
				else
					return true;
			}
			set { SetExtendedAttribute( "EnableRatings", value.ToString() ); }
		}

		public bool EnableDisclaimer
		{
			get
			{
				string val = GetExtendedAttribute( "EnableDisclaimer" );
				if(val != string.Empty)
					return bool.Parse(val);
				else
					return false;
			}
			set { SetExtendedAttribute( "EnableDisclaimer", value.ToString() ); }
		}

		public string RestrictedFileExtensions
		{
			get
			{
				return GetExtendedAttribute("RestrictedFileExtensions");
			}
			set
			{
				SetExtendedAttribute("RestrictedFileExtensions", value);
			}
		}

		public string AllowedFileExtensions
		{
			get
			{
				return GetExtendedAttribute("AllowedFileExtensions");
			}
			set
			{
				SetExtendedAttribute("AllowedFileExtensions", value);
			}
		}

		public bool EnableExternalLinks
		{
			get
			{
				string val = GetExtendedAttribute( "EnableExternalLinks" );
				if(val != string.Empty)
					return bool.Parse(val);
				else
					return true;
			}
			set 
			{ 
				SetExtendedAttribute( "EnableExternalLinks", value.ToString() );
			}
		}

		#endregion

		private static readonly string FileExtensionBasePattern = "^.+\\.(?:{0})$";
		private Regex _restrictedFileExtensionPattern = null;
		private Regex _allowedFileExtensionPattern = null;
		public bool IsFilenameValid(string fileName)
		{
			if (!Globals.IsNullorEmpty(RestrictedFileExtensions))
			{
				if (_restrictedFileExtensionPattern == null)
					_restrictedFileExtensionPattern = new Regex(String.Format(Folder.FileExtensionBasePattern, RestrictedFileExtensions.Replace(',','|')), RegexOptions.IgnoreCase|RegexOptions.CultureInvariant);
				
				if (_restrictedFileExtensionPattern.IsMatch(fileName))
					return false;
			}

			if (!Globals.IsNullorEmpty(AllowedFileExtensions))
			{
				if (_allowedFileExtensionPattern == null)
					_allowedFileExtensionPattern = new Regex(String.Format(Folder.FileExtensionBasePattern, AllowedFileExtensions.Replace(',','|')), RegexOptions.IgnoreCase|RegexOptions.CultureInvariant);
				
				if (!_allowedFileExtensionPattern.IsMatch(fileName))
					return false;
			}

			return true;
		}

		public override string Url
		{
			get
			{
				return FileGalleryUrls.Instance().ViewFolder(this.ApplicationKey);
			}
		}


	}
}
